<?php
	/**
	 Template Name:postListPage
	 */
	get_header();
	
	$nowcatid = $_GET["nowcatid"];

	$siteurl = get_bloginfo('template_url');
	$pageSize = 10 ;
	$pageModel= 10 ; //不允许改变的值
	
	//网站355
	$selfPageLink = "?page_id=355&nowcatid=".$nowcatid; //自身链接；
	$autoLink = "" ;
	
	$paged = $_POST["paged"]; 
	if($paged == null || "" == $paged ){
		$paged = $_GET["paged"]; 
		if($paged == null || "" == $paged ){
			$paged = 0 ;
		}
	}
	$statNum = $pageSize * $paged ;
	//$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); 

 	//if (0 < $numposts) $numposts = number_format($numposts);
 	$numposts =  wt_get_category_count($nowcatid);   
	global $wpdb;
get_header();
	
?>
	<div id="listmain" >
		<div id="listcont">
			<h2>NEWS,EVENTS AND ANNOUNCEMENTS</h2>
			<?php 
				  $nowcatid = $_GET["nowcatid"];	
					$postsql = "select post.guid as guid ,post.ID as ID,post.post_author as post_author,post.post_date as post_date,post.post_title as post_title,term.term_id as term_id ,post.post_content as post_content from wp_nwkvhq_terms term ,wp_nwkvhq_term_relationships ships, wp_nwkvhq_posts post where term.term_id = ships.term_taxonomy_id and post.ID = ships.object_id and term.term_id=$nowcatid  ORDER BY post.post_date DESC LIMIT $statNum,$pageSize"; 	 
					$myposts = $wpdb->get_results($postsql);
					foreach($myposts as $mypost) {
						$imgurl = get_first_image($mypost->post_content);
						$title= $mypost->post_title; 
			?>
					<div class="list">
						<span class="listcentimg"><a href="<a href="<?php echo $mypost->guid; ?>&nowcatid=<?php echo $nowcatid ?>" title="<?php echo $title?>">
							<img src="<?php echo $imgurl?>"alt="<?php echo $title; ?>"title="<?php echo $title; ?>" onload="javascript:DrawImage(this,265,187)" class="listimg" />
						</a></span>
						<h3 class="leftmarg"><a href="<?php echo $mypost->guid; ?>&nowcatid=<?php echo $nowcatid ?>"><?php echo $title; ?></a></h3>
						<span class="postdate leftmarg">
							<?php echo $mypost->post_date ?>
						</span>
						<p class="leftmarg"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $mypost->post_content)), 0, 120,"…"); ?></p>
						<span class="readmore leftmarg"><a href="<?php echo $mypost->guid; ?>&nowcatid=<?php echo $nowcatid ?>">>>Read More</a></span>
					</div>
			<?php 
				}
			?>
        	<div id="fenye">
           		<ul>
           		<?php
					
					$totalPage = ceil($numposts/$pageSize) ;
					if($totalPage >0){
						$i = 0 ;
						$pagedLink = $selfPageLink;
						if(intval($paged) > 0 ){		
					?>
							<li id="topnum" class="topnext"><a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>">Previous</a></li>
					<?php 
						}
					?>
					<?php 
						if($totalPage<=$pageModel){
						 	for(;$i<$totalPage;$i++){
						 		if($i == intval($paged)){
					?>
									<li  id="dangqiannum"><?php echo ($i +1);?></li>
					<?php 
						 		}else{
					?>
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 
						 		}
						 	}
						 		
						}elseif($totalPage < $pageModel + 3){
							$halfPageModel = intval($pageModel /3)  ;
							if(intval($paged) < $halfPageModel*2){
								
								for($i=0;$i < $halfPageModel*2+1;$i++){
									if($i == intval($paged)){
					?>
										<li  id="dangqiannum"><?php echo ($i +1);?></li>
					<?php 
									}else{
					?>
										<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 
									}
								}
					?>
								<li class="fennum"><a><?php echo "...";?></a></li>
					<?php 		
								for($i = $totalPage-($pageModel-$halfPageModel*2 -2);$i<$totalPage;$i++){
					?>
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 				
								}	
					?>
					<?php 
							}elseif(intval($paged) > $halfPageModel - 1) {
								for($i =0 ;$i< $halfPageModel-1;$i++){
					?>
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 				
								}
					?>
								<li class="fennum"><a><?php echo "...";?></a></li>
					<?php 	
								for($i=$totalPage-($pageModel-$halfPageModel);$i < $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 
									}
								}
							}
						}else{
							$halfPageModel = intval($pageModel /2)  ;
							if(intval($paged) < $halfPageModel-1){
								for($i =0 ;$i< $halfPageModel;$i++){
									if($i == intval($paged)){
					?>
										<li  id="dangqiannum"><?php echo ($i +1);?></li>
					<?php 
									}else{
					?>
										<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 
									}
								}
					?>
								<li class="fennum"><a><?php echo "...";?></a></li>
					<?php 			
								for($i =$totalPage - ($pageModel - $halfPageModel - 1) ;$i< $totalPage ;$i++){
					?>
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php				
								}
							}elseif(intval($paged) > $totalPage - ($halfPageModel) ) {
								for($i = 0 ;$i< $halfPageModel-1 ;$i++){
					?>
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php				
								}
					?>
								<li class="fennum"><a><?php echo "...";?></a></li>
					<?php 		
								for($i = $totalPage - ($pageModel - $halfPageModel) ;$i< $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<li  id="dangqiannum"><?php echo ($i +1);?></li>
					<?php 
									}else{
					?>
										<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><?php echo ($i +1);?></a></li>
					<?php 
									}
								}	
								
							}else {
					?>
								<li class="fennum"><a href="<?php echo $pagedLink."&paged=0"; ?>"><?php echo 1;?></a></li>
								<li class="fennum"><a href="<?php echo $pagedLink."&paged=1"; ?>"><?php echo 2;?></a></li>
					<?php 
								$uses = 3 ;
								if($paged > $totalPage/2){
									$uses = 2 ;
					?>
									 
									<li class="fennum"><a href="<?php echo $pagedLink."&paged=1"; ?>"><?php echo 3;?></a></li>
					<?php				
								}
					?>
								<li class="fennum"><a><?php echo "...";?></a></li>
								<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($paged)-1); ?>"><?php echo intval($paged);?></a></li>
								<li id="dangqiannum"><?php echo (intval($paged) +1);?></li>
								<li class="fennum"><a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>"><?php echo (intval($paged) +2);?></a></li>
								<li id="dangqiannum"><a><?php echo "...";?></a></li>
					<?php 		
								for($i = $totalPage - $uses ;$i< $totalPage;$i++){
					?>
										<li class="<?php echo $pagedLink."&paged=".(intval($i)); ?>"><a href="#"><?php echo ($i +1);?></a></li>
					<?php 
								}
							}
						}						
						if(intval($paged)<$totalPage-1){
					?>
							<li id="nextnum" class="topnext"><a href="<?php echo $pagedLink."&paged=".(intval($paged)+1); ?>">Next</a></li>
					<?php 
						}
					}
					?>
           		</ul>
           		<form method="get" action="" id="pageform">
            		<label for="pagenum" id="textlabel">Go to Page:</label>&nbsp;
             		<input type="text" name="pagenum" id="pagenum" />&nbsp;
               		<input type="submit" value="Go" id="fensubmit" />&nbsp;
          		</form>
     		</div>
  		</div>
  		<div id="listsieder">
      		<h2 class="main_siderT" id="listsiderT">AFFILIATES</h2>
      		<?php		
				$huanposts = get_posts ( "orderby=rand&numberposts=5&cat".$nowcatid ); 
				foreach ( $huanposts as $post ) {
			 ?>
     		<ul class="yborder">
		       	<li class="lititle"><a href="<?php echo get_permalink ( $post->ID ); ?>&nowcatid=<?php echo $nowcatid ?>" title="<?php echo $post->post_title ?>"><?php echo $post->post_title ?></a></li>
		      	<li class="licont"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 60,"…"); ?></li>
        	</ul>
        	<?php } ?>
           	<div id="lsliimg" >
           	<?php
				$huanposts = get_posts('showposts=6&orderby=rand&cat='.$nowcatid);
				foreach ( $huanposts as $post ) {
			?>
     			<ul>
              		<li class="lititle"><a href="<?php echo get_permalink ( $post->ID ); ?>&nowcatid=<?php echo $nowcatid ?>" title="<?php echo $post->post_title ?>"><?php echo $post->post_title ?></a></li>
               		<li class="maincont_img"><a href="#"><img src="<?php echo get_first_image($post->post_content)?>" width="116" height="71" alt="<?php echo $post->post_title ?>" title="<?php echo $post->post_title ?>" /></a></li>
              		<li class="miancont_cont"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 30,"…"); ?></li>
            	</ul>
        	<?php } ?>
         	</div>
    	</div>
    </div>

<?php  
	get_footer();
?>